import {actionTypes} from './index'
import http from '@/http'
import * as constant from '@/config/constant'
import {message} from 'antd'
import {actionFunc as submitBtnActionFunc} from '@/components/SubmitBtn/store'

const getValidateFieldsParams = (value) => {
    let defaultParams = {
        pppoeValidate: false,
        staticIpValidate: false,
        dhcpValidate: false,
    }
    switch (value) {
        case '1':
            defaultParams.pppoeValidate = true
            break;
        case '2':
            defaultParams.staticIpValidate = true
            break;
        case '3':
            defaultParams.dhcpValidate = true
            break;
        default:
    }
    return defaultParams
}
const changeFormData = (value) => ({
    type: actionTypes.CHANGE_FORM_DATA,
    value,
})

export const updateListData = (value) => ({
    type: actionTypes.UPDATE_LIST_DATA,
    value
})
export const updateValidateField = (value) => {
    //获取需要修改的校验字段参数
    let params = getValidateFieldsParams(value)
    params.connectTypeValueForWeb = value
    return {
        type: actionTypes.UPDATE_LIST_DATA,
        value: params
    }
}

export const requestList = (wanPort) => {
    return (dispatch) => {
        let url
        url = wanPort === 'wan0'? '/basicSet/wan0SetInit': '/basicSet/wan1SetInit';
        http.get(url).then(res => {
            res.enabled = res.formData.enabled === 1 ? true : false
            dispatch(updateListData(res))
            dispatch(updateValidateField(res.formData.connectType))
        }).catch(err => {
            console.log(err)
        })
    }
}

export const setParams = (params) => {
    return (dispatch) => {
        dispatch(submitBtnActionFunc.activateLoading())
        params.enabled = params.enabled ? 1 : 0;
        http.post('/basicSet/wanSet', params).then(
            res => {
                dispatch(submitBtnActionFunc.closeLoading())
                if (res.status === 1) {
                    dispatch(changeFormData(res.data))
                }
                message.success('Operate Success!')
            }).catch(err => {
            dispatch(submitBtnActionFunc.closeLoading())
        })
    }
}
export const setWanPort = (wanPort) => ({
    type: actionTypes.SET_WAN_PORT,
    value: wanPort
})
